﻿

cc.Class({
    extends: cc.Component,

    properties: {
        startTime:0,
        canvasNode: cc.Node,
        //玩家节点.
        playerNode:{
            default: null,
            type: cc.Node
        },
        cameraNode:{
            default: null,
            type: cc.Node
        },
        scoreLabel:{
            default: null,
            type: cc.Label
        },
        //时间Icon的节点.
        timeIconNode:{
            default: null,
            type: cc.Node
        },
        numNode:{
            default: null,
            type: cc.Node
        },
        rankDisplay: cc.Sprite,
        scoreManagerNode: cc.Node,
        bgmSourceNode: cc.Node,
		pausePanelNode: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        if(!Global.isFirstIn){
            //新手提示不可以开启触摸控制.
            this.setTouchControl();
        }
        
        //启用物理系统.
        cc.director.getPhysicsManager().enabled = true;
        //玩家的Player组件.
        this.player = this.playerNode.getComponent('Player');
        //剩余时间.
        this.timeLeft = this.startTime;
        this.cameraFollow = this.cameraNode.getComponent('CameraFollow');
        //分数.
        this.score = 0;
        this.combo = 0;
		Global.currentFloor = 0;
        //combo.
        this.comboManager = this.numNode.getComponent('ComboManager');

        //是否游戏结束.
        this.isAlive = true;

        this.scoreManager = this.scoreManagerNode.getComponent('ScoreTextManager');

        this.bgmSourceNode.active = Global.bgmOn;
    },

    start () {
        //开始时计分清零.
        Global.score = 0;
        this.showScoreText(0);
        this.rankTex = new cc.Texture2D();
    },

    update (dt) {
        if(Global.debug) return;
        this.updaetSubDomainCanvas();
    },

    //更新子域的最高分显示.
    updaetSubDomainCanvas () {
        if (!this.rankTex || this.isAlive) {
            return;
        }
        var openDataContext = wx.getOpenDataContext();
        var sharedCanvas = openDataContext.canvas;
        this.rankTex.initWithElement(sharedCanvas);
        this.rankTex.handleLoadedTexture();
        this.rankDisplay.node.setContentSize(cc.director.getVisibleSize());
        this.rankDisplay.spriteFrame = new cc.SpriteFrame(this.rankTex);
    },

    //设置屏幕触控.
    setTouchControl: function(){
        this.node.on('touchstart',this.onTouchStart,this);
    },

    onTouchStart: function(e){
        this.cameraFollow.doMove();
        //屏幕坐标系转换到世界坐标系.
        // var touchWorldPosition = this.cameraNode.convertToNodeSpaceAR(e.getLocation());
        // touchWorldPosition = cc.v2(touchWorldPosition.x,2*this.cameraNode.getPositionY() + touchWorldPosition.y);
        //改成控制左右屏幕往不同的随机方向跳跃.
        //跳跃. 
        if(!this.isAlive) return;
        this.player.doAction(1);
    },

    //显示分数文字.
    showScoreText(score){
        this.scoreLabel.string = '\n'+score.toString()+'\n';
        //给文字加一个抖动动画.
        var shakeAction = cc.sequence(cc.scaleTo(0.2,1.5).easing(cc.easeBackInOut()),cc.scaleTo(0.2,1));
        this.scoreLabel.node.runAction(shakeAction);
    },

    //收集到了金币.
    collectCoin(){
        //这里从收集金币改.
        // this.score++;
        // Global.score = this.score;
        // this.showScoreText(this.score);
    },

    //增加分数.
    addScore(count){
        this.score += count;
        Global.score = this.score;
        this.showScoreText(this.score);
        //显示加分.
        this.scoreManager.showAddScore(count, cc.v2(this.playerNode.getPositionX(), this.playerNode.getPositionY() + 100));
    },

    //连续跳跃的combo.
    scoreCombo(){
        //最大combo：9
        if(this.combo == 9) return;
        this.combo++;
        this.comboManager.showNumChange(this.combo - 1, this.combo);
    },

    //清空连击.
    clearCombo(){
        this.comboManager.showNumChange(this.combo, 0);
        this.combo = 0;
    },
	
	pauseGame: function(){
		if(!this.isAlive) return;
		cc.director.pause();
		this.pausePanelNode.active = true;
	},
	
	resumeGame: function(){
		cc.director.resume();
		this.pausePanelNode.active = false;
	}
});
